﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows.Data;
using GalaSoft.MvvmLight.Command;
using Northwind.Model;
using Northwind.Application;

namespace Northwind.ViewModel
{
    public class MainWindowViewModel
    {
        private readonly IUIDataProvider _dataProvider;

        private RelayCommand _showDetailsCommand;
        public RelayCommand ShowDetailsCommand
        {
            get
            {
                return _showDetailsCommand ??
                       (_showDetailsCommand =
                        new RelayCommand(
                            ShowCustomerDetails,
                            IsCustomerSelected));
            }
        }

        private string _selectedCustomerID;
        public string SelectedCustomerID
        {
            get { return _selectedCustomerID; }
            set
            {
                _selectedCustomerID = value;
                ShowDetailsCommand.RaiseCanExecuteChanged();
            }
        }

        private IList<Customer> _customers;
        public IList<Customer> Customers
        {
            get
            {
                if (_customers == null)
                {
                    GetCustomers();
                }
                return _customers;
            }
        }

        public ObservableCollection<ToolViewModel>
            Tools { get; set; }

        public MainWindowViewModel(
            IUIDataProvider dataProvider)
        {
            _dataProvider = dataProvider;
            Tools = new ObservableCollection<ToolViewModel>();
        }

        public void ShowCustomerDetails()
        {
            if (!IsCustomerSelected())
                throw new InvalidOperationException(
                    "Unable to show customer because no "
                    + "customer is selected.");

            CustomerDetailsViewModel customerDetailsViewModel
                = GetCustomerDetailsTool(SelectedCustomerID);
            if (customerDetailsViewModel == null)
            {
                customerDetailsViewModel
                    = new CustomerDetailsViewModel(
                        _dataProvider, SelectedCustomerID);
                Tools.Add(customerDetailsViewModel);
            }
            SetCurrentTool(customerDetailsViewModel);
        }

        public bool IsCustomerSelected()
        {
            return !string.IsNullOrEmpty(SelectedCustomerID);
        }

        private CustomerDetailsViewModel GetCustomerDetailsTool(
            string customerID)
        {
            return Tools
                .OfType<CustomerDetailsViewModel>()
                .FirstOrDefault(c =>
                                c.Customer.CustomerID ==
                                customerID);
        }

        private void SetCurrentTool(ToolViewModel currentTool)
        {
            ICollectionView collectionView =
                CollectionViewSource.GetDefaultView(Tools);
            if (collectionView != null)
            {
                if (collectionView.MoveCurrentTo(currentTool) !=
                    true)
                {
                    throw new InvalidOperationException(
                        "Could not find the current tool.");
                }
            }
        }

        private void GetCustomers()
        {
            _customers =
                _dataProvider.GetCustomers();
        }
    }
}